package com.wude.guild;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.umeng.analytics.MobclickAgent;
import com.wude.guild.adapter.PayAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;

import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 充值
 */
public class ChongActivity extends TitleBaseActivity {
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.group)
    RadioGroup group;
    @BindView(R.id.ptb)
    RadioButton ptb;
    @BindView(R.id.zhekou)
    RadioButton zhekou;
    @BindView(R.id.xian)
    ImageView xian;
    private int huang;
    private int hei;
    private int screenWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chong);
        ButterKnife.bind(this);

        setHeaderTitle("账户充值");
        init();
        setUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void init() {
        huang = x.app().getResources().getColor(R.color.juhuang);
        hei = x.app().getResources().getColor(R.color.text_5050);
        ptb.setTextColor(huang);
        zhekou.setTextColor(hei);

        //获取屏幕的宽度
        DisplayMetrics outMetrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;

        //设置mTabLine宽度//获取控件的(注意：一定要用父控件的LayoutParams写LinearLayout.LayoutParams)
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) xian.getLayoutParams();//获取控件的布局参数对象
        lp.width = screenWidth / 2;
        xian.setLayoutParams(lp); //设置该控件的layoutParams参数
    }

    private void setUI() {
        vp.setAdapter(new PayAdapter(getSupportFragmentManager()));
        vp.setOffscreenPageLimit(1);
        vp.setOnPageChangeListener(new zhifu());
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.ptb:
                        vp.setCurrentItem(0, false);
                        ptb.setTextColor(huang);
                        zhekou.setTextColor(hei);
                        break;
                    case R.id.zhekou:
                        vp.setCurrentItem(1, false);
                        ptb.setTextColor(hei);
                        zhekou.setTextColor(huang);
                        break;

                }
            }
        });
    }

    class zhifu implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) xian.getLayoutParams();
            //获取组件距离左侧组件的距离
            lp.leftMargin = (int) ((positionOffset + position) * screenWidth / 2);
            xian.setLayoutParams(lp);
        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    ptb.setChecked(true);
                    break;
                case 1:
                    zhekou.setChecked(true);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

}
